package com.example.abc;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取表单参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 简单验证（实际应用中应与数据库中的数据进行比对）
        if ("admin".equals(username) && "password".equals(password)) {
            // 验证成功，创建会话
            HttpSession session = request.getSession();
            session.setAttribute("username", username);

            // 重定向到欢迎页面
            response.sendRedirect("welcome.jsp");
        } else {
            // 验证失败，返回登录页面并显示错误信息
            request.setAttribute("error", "用户名密码错误");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }
}
